﻿using DeviceAlarmService.Models;
using DeviceAlarmService.Models.ModbusTcp;

namespace DeviceAlarmService.DeviceManagement.DeviceConnection.ModbusTcp;

public class ConfigurationGeneration
{
    private Models.ModbusTcp.DeviceInformation _deviceInformation = new();

    public Models.ModbusTcp.DeviceInformation deviceInformation
    {
        get
        {
            return _deviceInformation;
        }
    }

    public void GenerationDeviceInformation(string number, string name, string protocol)
    {
        _deviceInformation.number = number;
        _deviceInformation.name = name;
        _deviceInformation.protocol = (Protocol)Enum.Parse(typeof(Protocol), protocol, true);
    }

    public void GenerationDeviceAddress(string ip, string port)
    {
        _deviceInformation.deviceAddress = new DeviceAddress()
        {
            ip = ip,
            port = short.Parse(port),
        };
    }

    public void GenerationContacts(string phoneNumber, string level, string enableDialing, string enableSms)
    {
        if (_deviceInformation.contacts == null)
        {
            _deviceInformation.contacts = new List<Contacts>();
        }
        _deviceInformation.contacts.Add(new Contacts()
        {
            phoneNumber = phoneNumber,
            level = (Level)Enum.Parse(typeof(Level), level, true),
            enableSms = bool.Parse(enableSms),
            enableDialing = bool.Parse(enableDialing)
        });
    }

    public void GenerationDataAddress(string functionCode, string slaveAddress, string startAddress, string numberOfPoints)
    {
        if (_deviceInformation.dataAddress == null)
        {
            _deviceInformation.dataAddress = new();
        }
        _deviceInformation.dataAddress.functionCode = (FunctionCode)Enum.Parse(typeof(FunctionCode), functionCode, true);
        _deviceInformation.dataAddress.slaveAddress = byte.Parse(slaveAddress);
        _deviceInformation.dataAddress.startAddress = ushort.Parse(startAddress);
        _deviceInformation.dataAddress.numberOfPoints = ushort.Parse(numberOfPoints);
    }

    public void GenerationDataAddress(List<bool> isEnable, List<string> alarmInformation)
    {
        if (_deviceInformation.dataAddress == null)
        {
            _deviceInformation.dataAddress = new();
        }
        _deviceInformation.dataAddress.isEnable = isEnable;
        _deviceInformation.dataAddress.alarmInformation = alarmInformation;

        if (isEnable.Count != alarmInformation.Count)
        {
            throw new("数据添加错误，报警状态数量与报警信息数量不一致");
        }

        //if (_deviceInformation.dataAddress.numberOfPoints != alarmInformation.Count)
        //{
        //    throw new("数据添加错误，报警地址数量与报警信息数量不一致");
        //}

        List<string> uid = new List<string>();
        foreach (var VARIABLE in isEnable)
        {
            var uuidN = Guid.NewGuid().ToString("N");

            if (uid.Contains(uuidN) == false)
            {
                uid.Add(uuidN);
            }
            else
            {
                uid.Add(DateTimeOffset.Now.ToUnixTimeSeconds().ToString());
            }
        }
        _deviceInformation.dataAddress.uid = uid;
    }

}